// pagesCollect/pages/collect/claim/claim.js
import { listTownShipVillage, getMgyArea, userClaim } from "../../../../api/collect"
import { baseUrl } from "../../../../api/request"

const app = getApp()

const scroll = {
  page: 1,
  pagesize: 20,
  canLoad: true,
  listEnd: false,
  triggered: false,
}

Page({
  data: {
    scroll: JSON.parse(JSON.stringify(scroll)),
    list: [],
    navData: {
      title: '乡村认领',
      level: 10
    },
    area: [{
      id: "330700000000",
      level: 2,
      name: "金华市"
    }],
    areaIndex: {
      2: 0
    },
    townShipAddress: '',
    navHeight: 32,
    navTop: 48,
    selectShow: true,
  },
  onLoad() {
    this.setData({
      navHeight: app.globalData.navHeight, //导航栏高度
      navTop: app.globalData.navTop, //导航栏距顶部距离
    });

    this.getArea(0, 2)
  },
  areaChg(e) {
    let index = e.currentTarget.dataset.index,
      level = e.currentTarget.dataset.level;
    if (level < 4) {
      this.getArea(index, level)
    } else {
      this.setData({
        ['areaIndex.' + level]: index
      })
    }
  },
  async getArea(index, level) {
    let { area, areaIndex } = this.data;
    let data = area;
    for (let i = 2; i < 5; i++) {
      if (i < level) {
        data = data[areaIndex[i]].list;
      } else if (i == level) {
        data = data[index];
      } else {
        delete areaIndex[i]
      }
    }
    let res = await getMgyArea({ id: data.id })
    data.list = res.data.data;
    this.setData({
      area: area,
      ['areaIndex.' + level]: index
    })
  },
  getlist() {
    if (!this.data.scroll.canLoad) {
      return false;
    }
    let { scroll, list } = this.data;
    scroll.canLoad = false;
    listTownShipVillage({
      townShipAddress: this.data.townShipAddress,
      pageNum: scroll.page,
      // pageSize: scroll.pagesize
      pageSize: 999,
    }).then(res => {
      scroll.triggered = false;
      let newlist = res.data.rows;
      scroll.canLoad = true;

      newlist.forEach(item => {
        if (item.villageImage) {
          item.image = baseUrl + item.villageImage.split(',')[0];
          item.chk = false
        }
      })

      list = newlist;
      if (newlist.length < scroll.pagesize) {
        this.setData({
          ['scroll.listEnd']: true
        })
      } else {
        scroll.page++
      }
      this.setData({ list })
    }, function (err) {
      scroll.triggered = false;
      scroll.canLoad = true;
      this.setData({ list })
    })
  },
  refresh() {
    this.setData({
      scroll: JSON.parse(JSON.stringify(scroll)),
      list: []
    })
    this.getlist()
  },
  getMore() {
    // console.log(e)
    if (this.data.scroll.canLoad && !this.data.scroll.listEnd) {
      this.getlist()
    }
  },
  chkChg(e) {
    let index = e.currentTarget.dataset.index;
    let { list } = this.data;
    list[index].chk = !list[index].chk;
    this.setData({ list })
  },
  claimSure() {
    let { list } = this.data;
    let arr = [];
    list.forEach(item => {
      if (item.chk) arr.push(item.villageId)
    })
    if (arr.lenght == 0) {
      wx.showToast({
        icon: 'none',
        title: '请选择要认领的乡村',
      })
      return false;
    }

    userClaim({
      villageId: arr.join(',')
    }).then(res => {
      wx.showToast({
        icon: 'none',
        title: '认领成功',
      })
      let pages = getCurrentPages();
      let prePage = pages[pages.length - 2];
      prePage.refresh()
      wx.navigateBack()
    })
  },
  addressSure() {
    let { areaIndex } = this.data;
    let list = JSON.parse(JSON.stringify(this.data.area)),
      isWhole = true,
      townShipAddressArr = ['浙江省'];

    for (let i = 2; i < 5; i++) {
      if (areaIndex[i] || areaIndex[i] == 0) {
        let area = list[areaIndex[i]];
        townShipAddressArr.push(area.name);
        list = area.list;
      } else if (!list || list.length == 0) {
        break;
      } else {
        isWhole = false;
        break;
      }

    }
    if (!isWhole) {
      wx.showToast({
        icon: 'none',
        title: '请选择完整地址',
      })
      return false;
    }
    this.setData({
      townShipAddress: townShipAddressArr.join(','),
      selectShow: false
    })
    this.getlist()
  },
  openSelect() {
    this.setData({
      selectShow: true
    })
  },
  closeContainer() {
    if (!this.data.townShipAddress) {
      wx.navigateBack({})
    }
    this.setData({
      selectShow: false
    })
  }
})